package xcmg.device.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.MaterialClassDO;
import xcmg.device.dao.mapper.MaterialClassMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.MaterialClassVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.ListUtil;
import yb.ecp.fast.infra.util.PageHelperPlus;
import yb.ecp.fast.infra.util.Ref;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * Created by 阿斯顿 on 2018/7/27.
 */
@Service
public class MaterialClassService {

    @Autowired
    private MaterialClassMapper materialClassMapper;

    @Autowired
        private FastGenClient fastGenClient;

    public ErrorCode insert(MaterialClassVO materialClassVO) {
        ActionResult<String> textResult=fastGenClient.textGuid();
        if(textResult.getCode()!=ErrorCode.Success.getCode()){
            return ErrorCode.IllegalArument;
        }
        materialClassVO.setCreateDate(new Date());
        materialClassVO.setUpdateDate(new Date());
        materialClassVO.setIsDel(false);
        materialClassVO.setId(textResult.getValue().toString());
        materialClassVO.setClassValue(getClassValue(materialClassVO.getpId(),materialClassVO.getOrgId()));
        MaterialClassDO materialClassDO=getDO(materialClassVO);
        Integer nameCount=materialClassMapper.nameCount(materialClassDO);
        if(nameCount>0){
            return ErrorCode.ClassNameDuplicated;
        }
        Integer codeCounts=0;
        if(materialClassDO.getClassCode()!=null){
            codeCounts=materialClassMapper.classCodeCount(materialClassDO);
        }
        if(codeCounts>0){
            return ErrorCode.MaterialsIsClassCode;
        }
        ErrorCode ret= materialClassMapper.add(materialClassDO)>0? ErrorCode.Success:ErrorCode.Failure;
        materialClassVO.setId(materialClassDO.getId());
        return ret;
    }
    private String getClassValue(String pId,String orgId){
        String classValue=materialClassMapper.getMaxValue(pId,orgId);
        if(classValue==null||classValue==""){
            if("0".equals(pId)){
                classValue="001";
            }else {
                MaterialClassDO materialClassDO=materialClassMapper.item(pId);
                classValue=materialClassDO.getClassValue()+"001";
            }
        }else {
            String endValue= classValue.substring(classValue.length()-3,classValue.length());
            String startValue= classValue.substring(0,classValue.length()-3);
            Integer intValue= Integer.parseInt(endValue)+1;
            if(intValue<10){
                classValue=startValue+"00"+String.valueOf(intValue);
            }if(intValue>=10 && intValue<100){
                classValue=startValue+"0"+String.valueOf(intValue);
            }if(intValue>=100){
                classValue=startValue+String.valueOf(intValue);
            }
        }
        return classValue;
    }



    private MaterialClassDO getDO(MaterialClassVO materialClassVO){
        if(materialClassVO==null){return null;}
        MaterialClassDO materialClassDO=new MaterialClassDO();
        materialClassDO.setId(materialClassVO.getId());
        materialClassDO.setOrgId(materialClassVO.getOrgId());
        materialClassDO.setpId(materialClassVO.getpId());
        materialClassDO.setMaterialCalssName(materialClassVO.getMaterialCalssName());
        materialClassDO.setClassCode(materialClassVO.getClassCode());
        materialClassDO.setClassValue(materialClassVO.getClassValue());
        materialClassDO.setIsDel(materialClassVO.getIsDel());
        materialClassDO.setCreateDate(materialClassVO.getCreateDate());
        materialClassDO.setCreateId(materialClassVO.getCreateId());
        materialClassDO.setUpdateDate(materialClassVO.getUpdateDate());
        materialClassDO.setUpdateId(materialClassVO.getUpdateId());
        return materialClassDO;
    }
    private MaterialClassVO getVO(MaterialClassDO materialClassDO){
        if(materialClassDO==null){return null;}
        MaterialClassVO materialClassVO=new MaterialClassVO();
        materialClassVO.setId(materialClassDO.getId());
        materialClassVO.setOrgId(materialClassDO.getOrgId());
        materialClassVO.setpId(materialClassDO.getpId());
        materialClassVO.setMaterialCalssName(materialClassDO.getMaterialCalssName());
        materialClassVO.setClassCode(materialClassDO.getClassCode());
        materialClassVO.setClassValue(materialClassDO.getClassValue());
        materialClassVO.setIsDel(materialClassDO.getIsDel());
        materialClassVO.setCreateDate(materialClassDO.getCreateDate());
        materialClassVO.setCreateId(materialClassDO.getCreateId());
        materialClassVO.setUpdateDate(materialClassDO.getUpdateDate());
        materialClassVO.setUpdateId(materialClassDO.getUpdateId());
        materialClassVO.setChildCount(materialClassDO.getChildCount());
        return materialClassVO;
    }

    public ErrorCode update(MaterialClassVO materialClassVO) {
        MaterialClassVO newVO=new MaterialClassVO();
        newVO.setId(materialClassVO.getId());
        newVO.setClassCode(materialClassVO.getClassCode());
        newVO.setMaterialCalssName(materialClassVO.getMaterialCalssName());
        newVO.setUpdateId(materialClassVO.getUpdateId());
        newVO.setUpdateDate(new Date());
        newVO.setOrgId(materialClassVO.getOrgId());
        MaterialClassDO materialClassDO=getDO(newVO);
        MaterialClassDO sourceDO=materialClassMapper.item(materialClassDO.getId());
        if(sourceDO==null)
        {
            return ErrorCode.IllegalArument;
        }
        sourceDO.setMaterialCalssName(materialClassDO.getMaterialCalssName());
        Integer nameCount=materialClassMapper.nameCount(sourceDO);
        if(nameCount>0){
            return ErrorCode.ClassNameDuplicated;
        }
        Integer codeCounts=0;
        if(materialClassDO.getClassCode()!=null){
            codeCounts=materialClassMapper.classCodeCount(materialClassDO);
        }
        if(codeCounts>0){
            return ErrorCode.MaterialsIsClassCode;
        }
        return materialClassMapper.update(materialClassDO)>0?ErrorCode.Success:ErrorCode.Failure;
    }

    public ErrorCode delete(MaterialClassVO materialClassVO, Ref<List<String>> classNameList) {
        MaterialClassDO materialClassDO = materialClassMapper.item(materialClassVO.getId());
        if (materialClassDO == null)
        {
            return ErrorCode.IllegalArument;
        }
        List<String> className = new ArrayList<>();

        List<MaterialClassDO> classDOList = materialClassMapper.listIdByValue(materialClassDO);

        if (ListUtil.isNullOrEmpty(classDOList))
        {
            return ErrorCode.IllegalArument;
        }

        for (MaterialClassDO classDO : classDOList)
        {
            int companyCount = materialClassMapper.countCompanyByType(classDO.getId());
            if (companyCount > 0)
            {
                className.add(classDO.getMaterialCalssName());
            }
        }

        if (!ListUtil.isNullOrEmpty(className))
        {
            classNameList.set(className);
            return ErrorCode.ClassIsUsed;
        }

        return materialClassMapper.deleteByValue(materialClassDO.getOrgId(), materialClassDO.getClassValue()) > 0 ? ErrorCode.Success : ErrorCode.Failure;

    }

    public List<MaterialClassVO> list(MaterialClassVO materialClassVO) {
        materialClassVO.setIsDel(false);
        List<MaterialClassDO> listDO=materialClassMapper.getByPId(materialClassVO);
        List<MaterialClassVO> voList = new ArrayList<>();
        if(listDO==null){
            return  voList;
        }
        for (MaterialClassDO materialClassDO:listDO
                ) {
            voList.add(getVO(materialClassDO));
        }
        return voList;
    }

    public List<MaterialClassVO> getMaterialClassTreeList(String orgId) {
        List<MaterialClassVO> voList = new ArrayList<>();
        MaterialClassVO queryMaterialClassVO = new MaterialClassVO();
        queryMaterialClassVO.setpId("0");
        queryMaterialClassVO.setOrgId(orgId);
        PageHelperPlus.orderBy("id desc");
        List<MaterialClassDO> listDO=materialClassMapper.getByPId(queryMaterialClassVO);
        for (MaterialClassDO materialClassDO: listDO ) {
            queryMaterialClassVO.setpId(materialClassDO.getId());
            List<MaterialClassDO> secondListDO = materialClassMapper.getByPId(queryMaterialClassVO);
            if(secondListDO.isEmpty()){
                continue;
            }
            List<MaterialClassVO> childMaterialClassVOList = new ArrayList<>();
            secondListDO.forEach(x->{
                MaterialClassVO childMaterialClassVO = new MaterialClassVO();
                BeanUtils.copyProperties(x,childMaterialClassVO);
                childMaterialClassVOList.add(childMaterialClassVO);
            });
            MaterialClassVO resultMaterialClassVO = new MaterialClassVO();
            BeanUtils.copyProperties(materialClassDO,resultMaterialClassVO);
            resultMaterialClassVO.setMaterialClassVOList(childMaterialClassVOList);
            voList.add(resultMaterialClassVO);
        }

        MaterialClassVO materialClassVO=new MaterialClassVO();
        materialClassVO.setMaterialCalssName("未设置");
        materialClassVO.setClassCode("01");
        materialClassVO.setId("1");
        List<MaterialClassVO> childMaterialClassVOList = new ArrayList<>();
        MaterialClassVO childMaterialClassVO=new MaterialClassVO();
        childMaterialClassVO.setMaterialCalssName("未设置");
        childMaterialClassVO.setClassCode("0101");
        childMaterialClassVO.setId("2");
        childMaterialClassVO.setpId("1");
        childMaterialClassVOList.add(childMaterialClassVO);
        materialClassVO.setMaterialClassVOList(childMaterialClassVOList);
        voList.add(materialClassVO);

        return voList;
    }

    public List<MaterialClassVO> getAllSecondlist(String orgId) {
        PageHelperPlus.orderBy("id desc");
        List<MaterialClassDO> listDO=materialClassMapper.getAllSecondlist(orgId);
        List<MaterialClassVO> voList = new ArrayList<>();
        if(listDO==null){
            return  voList;
        }
        for (MaterialClassDO materialClassDO:listDO) {
            voList.add(getVO(materialClassDO));
        }
        MaterialClassVO materialClassVO=new MaterialClassVO();
        materialClassVO.setMaterialCalssName("未设置");
        materialClassVO.setClassCode("000");
        voList.add(materialClassVO);

        return voList.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MaterialClassVO::getMaterialCalssName))), ArrayList::new));
    }
}
